import 'package:flutter_admin/pages/common/cry/cry.dart';
import 'package:flutter_admin/pages/common/cry/cry_button_bar.dart';
import 'package:flutter_admin/pages/common/cry/cry_buttons.dart';
import 'package:flutter_admin/pages/common/cry/cry_dialog.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/form/index.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/cry_data_table.dart';
import 'package:flutter_admin/api/odoo/base_stationuser_odoo_api.dart';
import 'package:flutter_admin/constants/enum.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/base_productionstation.dart';
import 'package:flutter_admin/models/odoo/base_stationuser.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/utils/adaptive_util.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:syncfusion_flutter_core/theme.dart';
import 'package:get/get.dart';

class BaseCoefficientList extends StatefulWidget {
  BaseCoefficientList({Key? key, this.baseProductionstation}) : super(key: key);
  final BaseProductionstation? baseProductionstation;
  @override
  _BaseCoefficientListState createState() => _BaseCoefficientListState();
}

class _BaseCoefficientListState extends State<BaseCoefficientList> {
  // final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  ArticleDataSource ds = ArticleDataSource();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  // PageModel page =
  //     PageModel(orders: [OrderItemModel(column: 'create_date', asc: false)]);
  BaseStationuser baseStationuser = BaseStationuser();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _query();
    });
  }

  @override
  Widget build(BuildContext context) {



    // var result = Scaffold(
    //   appBar: AppBar(
    //     title: Text('分配系数'),
    //     actions: [
    //       CryButton(
    //         iconData: Icons.save,
    //         label: S.of(context).save,
    //         onPressed: () => null,
    //       ),
    //     ],
    //   ),
    //   key: scaffoldStateKey,
    //   body: Column(
    //     mainAxisAlignment: MainAxisAlignment.center,
    //     crossAxisAlignment: CrossAxisAlignment.start,
    //     children: <Widget>[
    //       SizedBox(height: 10),
    //       isDisplayDesktop(context) ? form : Container(),
    //       isDisplayDesktop(context) ? buttonBar : Container(),
    //       Expanded(child: SingleChildScrollView(child: table)),
    //     ],
    //   ),
    // );
    // return result;
    var dataGrid = SfDataGrid(
      source: ds,
      columns: <GridColumn>[
        GridTextColumn(
          columnName: 'username',
          label: Container(
            padding: EdgeInsets.all(8.0),
            alignment: Alignment.centerLeft,
            child: Text(
              '人员名称',
              overflow: TextOverflow.ellipsis,
            ),
          ),
          columnWidthMode: ColumnWidthMode.fill,
        ),
        GridTextColumn(
          columnName: 'job',
          label: Container(
            padding: EdgeInsets.all(8.0),
            alignment: Alignment.centerLeft,
            child: Text(
              '岗位',
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ),
        GridTextColumn(
          columnName: 'writeUid',
          label: Container(
            padding: EdgeInsets.all(8.0),
            alignment: Alignment.centerLeft,
            child: Text(
              '系数',
              overflow: TextOverflow.ellipsis,
            ),
          ),
          width: 120,
        ),
      ],
    );
    var pager = SfDataPagerTheme(
      data: SfDataPagerThemeData(
        brightness: Brightness.light,
        selectedItemColor: Get.theme.primaryColor,
      ),
      child: SfDataPager(
        delegate: ds,
        pageCount: 10,
        direction: Axis.horizontal,
      ),
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text('分配系数'),
        actions: [
          CryButton(
            iconData: Icons.save,
            label: S.of(context).save,
            onPressed: () => null,
          ),
        ],
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(child: dataGrid),
          pager,
        ],
      ),
    );
    return result;
  }

  _query() {
    baseStationuser.stationId = widget.baseProductionstation!.id;
    // formKey.currentState!.save();
    ds.loadData(baseStationuser: baseStationuser);
  }

  // reset() async {
  //   article = Article();
  //   formKey.currentState!.reset();
  //   await ds.loadData(params: {});
  // }

  // _edit(BaseStationuser? baseStationuser) async {
  //   showDialog(
  //     context: context,
  //     builder: (BuildContext context) => Dialog(
  //       child: BaseStationuserEdit(
  //         baseStationuser: baseStationuser,
  //       ),
  //     ),
  //   ).then((v) {
  //     if (v != null) {
  //       _query();
  //     }
  //   });
  // }
  //
  // _reset() {
  //   baseStationuser = BaseStationuser();
  //   formKey.currentState!.reset();
  //   _loadData();
  // }
  //
  // _query() {
  //   formKey.currentState?.save();
  //   tableKey.currentState?.pageTo(0);
  //   _loadData();
  // }
  //
  // _loadData() async {
  //   baseStationuser.stationId = widget.baseProductionstation!.id;
  //   ResponseBodyApi responseBodyApi = await BaseStationuserOdooApi.alreadyPage(
  //       data: {"page": page, "domainList": baseStationuser.toDomainListToo()});
  //   if (responseBodyApi.success!) {
  //     page = responseBodyApi.data != null ? responseBodyApi.data : PageModel();
  //     tableKey.currentState!.loadData(page);
  //     //_getSelectList();
  //   }
  // }
  //
  // _delete(List<BaseStationuser> selectedList) async {
  //   cryConfirm(context, S.of(context).confirmDelete, (context) async {
  //     List ids = selectedList.map((v) {
  //       return int.parse(v.id!);
  //     }).toList();
  //     var result = await BaseStationuserOdooApi.removeByIds(ids);
  //     if (result.success!) {
  //       _query();
  //       CryUtils.message(S.of(Cry.context).success);
  //     }
  //   });
  // }
  //
  // _search() {
  //   return showDialog(
  //     context: context,
  //     builder: (BuildContext context) => Dialog(
  //       child: BaseStationuserDialog(),
  //     ),
  //   ).then((v) {
  //     if (v != null) {
  //       baseStationuser = v;
  //       _query();
  //     }
  //   });
  // }
  //
  // _operationList() {
  //   return PopupMenuButton(
  //     onSelected: (dynamic v) {
  //       if (v == OperationType.query) {
  //         _search();
  //       } else if (v == OperationType.add) {
  //         _edit(null);
  //       } else if (v == OperationType.edit) {
  //         _edit(selectedList[0]);
  //       } else if (v == OperationType.delete) {
  //         _delete(selectedList);
  //       }
  //     },
  //     itemBuilder: (context) => <PopupMenuEntry<OperationType>>[
  //       // PopupMenuItem<OperationType>(
  //       //   value: OperationType.query,
  //       //   child: ListTile(
  //       //     leading: const Icon(Icons.search),
  //       //     title: Text('查询'),
  //       //   ),
  //       // ),
  //       // PopupMenuItem<OperationType>(
  //       //   value: OperationType.add,
  //       //   child: ListTile(
  //       //     leading: const Icon(Icons.add),
  //       //     title: Text('添加'),
  //       //   ),
  //       // ),
  //       // PopupMenuItem<OperationType>(
  //       //   value: OperationType.edit,
  //       //   enabled: selectedList.length == 1,
  //       //   child: ListTile(
  //       //     leading: const Icon(Icons.edit),
  //       //     enabled: selectedList.length == 1,
  //       //     title: Text('修改'),
  //       //   ),
  //       // ),
  //       // PopupMenuItem<OperationType>(
  //       //   value: OperationType.delete,
  //       //   enabled: selectedList.length != 0,
  //       //   child: ListTile(
  //       //     leading: const Icon(Icons.delete), //Icons.delete
  //       //     enabled: selectedList.length != 0,
  //       //     title: Text('删除'),
  //       //   ),
  //       // ),
  //     ],
  //   );
  // }
}

class ArticleDataSource extends DataGridSource {
  PageModel pageModel = PageModel();
  List<DataGridRow> _rows = [];

  loadData({BaseStationuser? baseStationuser}) async {
    ResponseBodyApi responseBodyApi = await BaseStationuserOdooApi.alreadyPage(
        data: {"page": pageModel, "domainList": baseStationuser!.toDomainListToo()});
    if (responseBodyApi.success!) {
      pageModel = responseBodyApi.data != null ? responseBodyApi.data : PageModel();
      List<BaseStationuser> list = pageModel.records.map((element) => BaseStationuser.fromMap(element)).toList();
      _rows = list.map<DataGridRow>((v) {
        return DataGridRow(cells: [
          DataGridCell(columnName: 'article', value: v),
        ]);
      }).toList(growable: false);
      notifyDataSourceListeners();
    }
  }

  @override
  List<DataGridRow> get rows => _rows;

  @override
  Future<bool> handlePageChange(int oldPageIndex, int newPageIndex) async {
    pageModel.current = newPageIndex + 1;
    await loadData();
    return true;
  }

  @override
  DataGridRowAdapter buildRow(DataGridRow row) {
    BaseStationuser baseStationuser = row.getCells()[0].value;
    return DataGridRowAdapter(cells: [
      Container(
        padding: const EdgeInsets.all(8),
        alignment: Alignment.centerRight,
        child: Text(
          baseStationuser.username!,
          overflow: TextOverflow.ellipsis,
        ),
      ),
      Container(
        padding: const EdgeInsets.all(8),
        alignment: Alignment.centerRight,
        child: Text(
          baseStationuser.job ?? '--',
          overflow: TextOverflow.ellipsis,
        ),
      ),
      Container(
        padding: const EdgeInsets.all(8),
        alignment: Alignment.centerRight,
        child: Text(
          baseStationuser.writeUid ?? '--',
          overflow: TextOverflow.ellipsis,
        ),
      ),
    ]);
  }

  // delete(ids) async {
  //   cryConfirm(Cry.context, S.of(Cry.context).confirmDelete, (context) async {
  //     if ((await ArticleApi.removeByIds(ids)).success!) {
  //       loadData();
  //       CryUtils.message(S.of(Cry.context).success);
  //     }
  //   });
  // }
  //
  // edit({Article? article}) async {
  //   var result = await Utils.fullscreenDialog(ArticleEdit(article: article));
  //   if (result ?? false) {
  //     loadData();
  //   }
  // }
}


